<%@page import="multiface.web.util.PagerList"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Iterator"%>
<%@page import="multiface.awwadco.jpa.Prodotto"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.RollbackException"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="multiface.awwadco.jpa.Categoria"%>
<%@page import="multiface.awwadco.jpa.File"%>
<%@page import="multiface.web.util.categories.AlberoCategorieProdotti"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<script type="text/javascript">
    $(document).ready(function(){
        $("#formBox").validate();
        nicEditors.allTextAreas({buttonList : ['bold','italic','underline','ul','ol']});
    });
    
    $("#homepage").live('click',function() {
        if($("#homepage:checked").val()=='on'){
            $('#homepage_val').val("true");
            $("#homepage_span").html('Show in HomePage');
        }else{
            $('#homepage_val').val("false");
            $("#homepage_span").html('Hidden in HomePage');
        }
    });
    
    $("#visibile_prezzo").live('click',function() {
        if($("#visibile_prezzo:checked").val()=='on'){
            $('#visibile_prezzo_val').val("true");
            $("#visibile_prezzo_span").html('Show in HomePage');
        }else{
            $('#visibile_prezzo_val').val("false");
            $("#visibile_prezzo_span").html('Hidden in HomePage');
        }
    });
    
    $("#offerta").live('click',function() {
        if($("#offerta:checked").val()=='on'){
            $('#offerta_val').val("true");
        }else{
            $('#offerta_val').val("false");
        }
    });
    
    
    $("#select_condition").change(function(data) {
        var sel = $("#select_condition option:selected").val();
        if(sel == "-1"){
             $("#condizioni").val("");
            $("#condizioni").attr("disabled", false);
        }else{
            $("#condizioni").val(sel);
            $("#condizioni").attr("disabled", true);
        }
    });
    
</script>


<%if (session.getAttribute("admin") == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>

<div class="box">
    <form class="formBox" id="formBox" method="post" action="">
        <div class="title">Add New Product</div>
        <br>

        <div class="inputs">
            <label for="nome">Name</label>
            <input name="nome" id="nome" value="" maxlength="255" class="required" type="text"/>
        </div>

        <div class="inputs">
            <label style="height:50px;margin-right: 6px;" for="nome">Short Description</label>
            <textarea cols="100" style="height:100px;" name="descrizione_homepage" id="descrizione_homepage"></textarea>
        </div>

        <div class="inputs">
            <label style="height: 50px;margin-right: 6px;" for="nome">Description</label>
            <textarea cols="100" style="height:200px;" name="descrizione" id="descrizione"></textarea>
        </div>

        <div class="inputs">
            <label for="modello">Model</label>
            <input name="modello" id="modello" class="required" maxlength="255" type="text"/>
        </div>

        <div class="inputs">
            <label for="condizioni">Conditions</label>
            <input name="condizioni" id="condizioni" class="required" maxlength="255" type="text"/>
            <select id="select_condition">
                <option value="-1">   </option>
                <option value="Excellent">Excellent</option>
                <option value="Good">Good</option>
                <option value="Refurbished">Refurbished</option>
            </select>
        </div>

        <div class="inputs">
            <label for="homepage">HomePage</label>
            <input style="width: 16px" name="homepage" id="homepage" type="checkbox"/>
            <span id="homepage_span" style="font-size: 10px; font-style: italic; color:#999;">Hidden in HomePage</span>
            <input type="hidden" name="homepage_val" id="homepage_val" value="false"/>
        </div>

        <div class="inputs">
            <label for="anno">Year</label>
            <select id="anno">
                <%
                    out.println("<option selected value=\"-1\">NONE</option>");
                    for (int i = 1960; i <= 2031; i++) {
                        out.println("<option value=\"" + i + "\">" + i + "</option>");
                    }
                %>
            </select>
        </div>

        <div class="inputs">
            <label for="codice">Code</label>
            <input name="codice" id="codice" class="required" type="text" maxlength="10"/>
        </div>

        <div class="inputs">
            <label for="moneta">Money</label>
            <select id="moneta">
                <option selected value="EURO">Euro (€)</option>
                <option value="DOLLARO">US Dollar ($)</option>
            </select>
        </div>

        <div class="inputs">
            <label for="prezzo">Price</label>
            <input name="prezzo" id="prezzo" type="text"/>
            <input style="width: 16px" name="visibile_prezzo" id="visibile_prezzo" type="checkbox"/>
            <span id="visibile_prezzo_span" style="font-size: 10px; font-style: italic; color:#999;">Hidden in HomePage</span>
            <input type="hidden" name="visibile_prezzo_val" id="visibile_prezzo_val" value="false"/>
        </div>

        <div class="inputs">
            <label for="pezzi">Pieces in Stock</label>
            <input name="pezzi" id="pezzi" type="text"/>
        </div>

        <div class="inputs">
            <label for="offerta">Promo</label>
            <input style="width: 16px" name="offerta" id="offerta" type="checkbox"/>
            <input type="hidden" name="offerta_val" id="offerta_val" value="false"/>
        </div>
        <input name="categoria" id="categoria" type="hidden" value="-1"/>
        <input name="ricambio" id="ricambio" type="hidden" value="false"/>
    </form>
</div>
<%
    AlberoCategorieProdotti tree = new AlberoCategorieProdotti();
    tree.caricaDaDB();
%>
<div class="box">
    <div class="title">Product Categories</div>
    <ul class="ul_open">
        <%
            List<Categoria> lcat = tree.categorieInOrdineDiProfondita();
            Iterator it = lcat.iterator();
            int sc = -1;
            int csc = 0;
            int p = 0;
            int[] arrpos = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

            while (it.hasNext()) {
                Categoria c = (Categoria) it.next();
                if (c.getSopracategoria() > sc) {
                    // entro in una sottocategoria
                    csc++;
                    p++;
                    sc = c.getSopracategoria();
                    //
                    out.println("<ul id=\"sottoli_" + sc + "\" class=\"ul_close\">");
                }
                if (c.getSopracategoria() < sc) {
                    //esco da una sottocategoira
                    int h = tree.Altezza(c);
                    //Sy_stem.out.println("[h=" + h + "]");
                    int m = (p - h) + 1;
                    for (int i = 0; i < m; i++) {
                        arrpos[p] = 0;
                        p--;
                        sc = c.getSopracategoria();
                        out.println("</ul>");
                    }
                }

                arrpos[p] = arrpos[p] + 1;
                String pre = "";
                for (int i = 0; i < p + 1; i++) {
                    pre += (arrpos[i] + "");
                    if (i < p) {
                        pre += ".";
                    }
                }
                out.println("<li class=\"li-cat\" id=\"li_cat_" + c.getIdcategoria() + "\"><a href=\"javascript:set_cat(" + c.getIdcategoria() + ");\">" + pre + " - " + c.getNome() + "</a></li>");
            }
        %>
    </ul>
</div>
